package com.example.hudongwx.myclass0719application;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.hudongwx.myclass0719application.Json.MyJsonObj;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(value = R.layout.activity_brow1)
public class Brow1Activity extends AppCompatActivity {
    String TAG="Brow1Activity";
       @ViewInject(R.id.brow1_listview)
       private ListView listView;
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        //接收传回来的数据
        String url=getIntent().getStringExtra("url");
      if (null!=url){
          webView=new WebView(this);
          webView.getSettings().setJavaScriptEnabled(true);
         webView.setWebViewClient(new WebViewClient(){
             @Override
             public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                 return true;
             }
         });
             getWebUrl(url);
          Log.i(TAG, "onCreate: ");

      }
    }
//点击返回
    @Event(R.id.brow1_imageview)
    private void back(View v){
        finish();
    }
    //请求网址的方法
    private void getWebUrl(String url){
      x.http().get(new RequestParams(API.getUrl(url)), new Callback.CommonCallback<MyJsonObj>() {
          @Override
          public void onSuccess(MyJsonObj result) {
              Log.i(TAG, "onSuccess: ");
                         listView.setAdapter(new Myadpter(result));
          }

          @Override
          public void onError(Throwable ex, boolean isOnCallback) {

          }

          @Override
          public void onCancelled(CancelledException cex) {

          }

          @Override
          public void onFinished() {

          }
      });
    }

    private  class Myadpter extends BaseAdapter{
          private JSONObject data;

        public Myadpter(JSONObject data) {
            this.data = data;
        }


        @Override
        public int getCount() {
            int len=0;
            try {
              len=  data.getJSONArray("data").length()+1;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return  len;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

             if (0==position){
                 try {
                String  adress=data.getJSONObject("brand").getString("brandUrl");
                     webView.loadUrl(adress);

                 } catch (JSONException e) {
                     e.printStackTrace();
                 }
                 return  webView;
             }else {
                 ImageView imageView=null;
                 if (null!=convertView&&convertView instanceof ImageView){
                     //复用布局
                     imageView=(ImageView)convertView;
                 }else {
                     //没有可用布局，直接创建一个
                     View itemView = getLayoutInflater().inflate(R.layout.layout_brow1, null);
                     imageView = (ImageView) itemView.findViewById(R.id.layout_brow_1);
                 }
                 try {

                     x.image().bind(imageView,data.getJSONArray("data").getJSONObject(position).getString("imgUrl"));

                 } catch (JSONException e) {
                     e.printStackTrace();

                 }
                 return imageView;
             }

        }
    }
}
